//
//  NovelDataBase.m
//  UI数据库
//
//  Created by dllo on 15/8/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "NovelDataBase.h"
#import "DB.h"

#import "NovelModel.h"
#import "CollectModel.h"
@implementation NovelDataBase
+ (void)insertWithModel:( NovelModel *)model
{
    
    
    //第一步:获得数据库指针
    sqlite3 *db = [DB addDB];
    
    // 第二步:获得数据库语句
    if (model.cityId == nil) {
        
    }else
    {
        
    NSString *str = [NSString stringWithFormat:@"insert into City(c_id,c_name,c_high,c_low,c_sky,c_backimage) values('%@','%@','%@','%@','%@','%@')",model.cityId,model.cityName,model.high,model.low,model.sky,model.backImage];
    
    
    //第三部:执行SQL语句
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK ) {
        NSLog(@"插入成功    %@",model.cityId);
        
    }else{
        NSLog(@"插入失败");
    }
    
    }
    
    
}



////删除
+ (void)deleteWithModel:(NovelModel *)model
{
    
    
    sqlite3 *db = [DB addDB];
    NSString *str = [NSString stringWithFormat:@"delete from City where c_id='%@'",model.cityId];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {	
        NSLog(@"删除成功%@",model.cityId);
    }else{
        NSLog(@"删除失败");
    }
}

//更新
//+(void)updataWithModel:(NovelModel *)model
//{
//    
//    sqlite3 *db = [DB addDB];
//    NSString *str = [NSString stringWithFormat:@"update Novel set (a_name = '%@' where a_id = '%@')",model.a_name,model.a_id];
//    
//    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
//    if (result == SQLITE_OK) {
//        NSLog(@"更新成功");
//    }else{
//        
//        
//        
//        NSLog(@"更新失败");
//    }
//    
//}
//查询所有的数据
+(NSArray *) selectAll
{
    
    
    //获得数据库的指针
    sqlite3 *db = [DB addDB];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    //数据库语句
    NSString *str = [NSString stringWithFormat:@"select * from City"];
  
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], - 1, &stmt, nil);
    
    NSMutableArray *arr = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        //判断是否下一行数据 可用于读取
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            
            const unsigned char *c_id   = sqlite3_column_text(stmt, 0);
            const unsigned char *c_name = sqlite3_column_text(stmt, 1);
            const unsigned char *c_high = sqlite3_column_text(stmt, 2);
            const unsigned char *c_low  = sqlite3_column_text(stmt, 3);
            
            
            const unsigned char *c_sky  = sqlite3_column_text(stmt, 4);
            
            
            const unsigned char *c_backImage = sqlite3_column_text(stmt, 5);
            
            
            NSString *cid = [NSString stringWithUTF8String:(const char *)c_id];
            NSString *cname = [NSString stringWithUTF8String:(const char *)c_name];
            NSString *chigh = [NSString stringWithUTF8String:(const char *)c_high];
            NSString *clow = [NSString stringWithUTF8String:(const char *)c_low];
            NSString *csky = [NSString stringWithUTF8String:(const char *)c_sky];
            NSString *cbackimage = [NSString stringWithUTF8String:(const char *)c_backImage];
            
            
            NovelModel *model = [NovelModel collectionWithCityId:cid cityName:cname high:chigh low:clow sky:csky backImage:cbackimage];
            [arr  addObject:model];

            
            }
    }else{
                    
            NSLog(@"查询失败");

    }
    
    sqlite3_finalize(stmt);
    return arr;
    
}

//查询单个的数据
+(BOOL) selectWith:(NSString *)name
{
    
    
    //获得数据库的指针
    sqlite3 *db = [DB addDB];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    //数据库语句
    NSString *str = [NSString stringWithFormat:@"select c_name from City where c_name='%@'",name];
    int result = sqlite3_prepare_v2(db, [str UTF8String], - 1, &stmt, nil);
    if (result == SQLITE_OK) {
        //判断是否下一行数据 可用于读取
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
//            const unsigned char *aid = sqlite3_column_text(stmt, 0);
            
         
            sqlite3_finalize(stmt);
            return YES;
            
            
            
            
        }
    }else{
        
        NSLog(@"查询失败");
        
    }
    
    sqlite3_finalize(stmt);
    return NO;
    
}
+ (BOOL)selectWithId:(NSString *)myId
{
    
    
    //获得数据库的指针
    sqlite3 *db = [DB addDB];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    //数据库语句
    NSString *str = [NSString stringWithFormat:@"select c_id from City where c_id='%@'",myId];
    int result = sqlite3_prepare_v2(db, [str UTF8String], - 1, &stmt, nil);
    if (result == SQLITE_OK) {
        //判断是否下一行数据 可用于读取
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
            //            const unsigned char *aid = sqlite3_column_text(stmt, 0);
            
            
            sqlite3_finalize(stmt);
            return YES;
            
            
            
            
        }
    }else{
        
        NSLog(@"查询失败");
        
    }
    
    sqlite3_finalize(stmt);
    return NO;
    
}
//collect 表
+ (void)insertCollectWithModel:(CollectModel *)model
{
    
    
    //第一步:获得数据库指针
    sqlite3 *db = [DB addDB];
    
    // 第二步:获得数据库语句
    if (model.title == nil) {
        
    }else{
        
        NSString *str = [NSString stringWithFormat:@"insert into Collect(c_image,c_title) values('%@','%@')",model.imageStr,model.title];
        //第三部:执行SQL语句
        
        int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
        
        if (result == SQLITE_OK ) {
            NSLog(@"插入成功");
            
        }else{
            NSLog(@"插入失败");
        }
        
    }
    
    
}
+ (NSArray *)selectCollectAll
{
    
    
    //获得数据库的指针
    sqlite3 *db = [DB addDB];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    //数据库语句
    NSString *str = [NSString stringWithFormat:@"select * from Collect"];
    
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], - 1, &stmt, nil);
    
    NSMutableArray *arr = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        //判断是否下一行数据 可用于读取
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            
            const unsigned char *c_image   = sqlite3_column_text(stmt, 0);
            const unsigned char *c_title = sqlite3_column_text(stmt, 1);
       
            
            
            NSString *cimage = [NSString stringWithUTF8String:(const char *)c_image];
            NSString *ctitle = [NSString stringWithUTF8String:(const char *)c_title];
            
            CollectModel *model = [CollectModel collectWithCollectImageStr:cimage title:ctitle];
            [arr  addObject:model];
            //            NSLog(@"从数据库获取成功!");
            
        }
    }else{
        
        NSLog(@"查询失败");
        
    }
    
    sqlite3_finalize(stmt);
    return arr;
    
}
+(BOOL) selectCollectWith:(NSString *)lableText
{
    
    
    //获得数据库的指针
    sqlite3 *db = [DB addDB];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    //数据库语句
    NSString *str = [NSString stringWithFormat:@"select c_image from Collect where c_image='%@'",lableText];
    int result = sqlite3_prepare_v2(db, [str UTF8String], - 1, &stmt, nil);
    if (result == SQLITE_OK) {
        //判断是否下一行数据 可用于读取
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
            //            const unsigned char *aid = sqlite3_column_text(stmt, 0);
            
            
            sqlite3_finalize(stmt);
            return YES;
            
            
            
            
        }
    }else{
        
        NSLog(@"查询失败");
        
    }
    
    sqlite3_finalize(stmt);
    return NO;
    
}
+ (void)deleteCollectWithModel:(CollectModel *)model
{
    
    
    sqlite3 *db = [DB addDB];
    NSString *str = [NSString stringWithFormat:@"delete from Collect where c_image='%@'",model.imageStr];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功%@",model.imageStr);
    }else{
        NSLog(@"删除失败");
    }
}

@end
